#!/opt/local/bin/perl -w

use strict;
use warnings;
use File::List;
use File::Basename;
use Imager;
use Imager::Filter::RoundedCorner;
use Template;

my $img = Imager->new();
my $img_s = Imager->new();
my $img_m = Imager->new();

my $output_dir = $ARGV[1];

mkdir "$ARGV[1]/thumbnail_s";
mkdir "$ARGV[1]/thumbnail_m";
mkdir "$ARGV[1]/js";
mkdir "$ARGV[1]/css";

my @photos;

my $search = new File::List( $ARGV[0] );
my @files = @{ $search->find( "\.jpg\$" ), $search->find( "\.JPG\$" ) };
@files = sort @files;

foreach my $photo ( @files ){

    my( $filename, $directories, $suffix ) = fileparse( $photo );

    push( @photos, $filename );
    
    $img->read( file=>$photo );
    $img_m = $img->scale( ypixels => 600 );    
    $img_s = $img;

    $img_s->filter(
        type   => 'rounded_corner',
        radius => 100,
        bg     => '#ffffff',
	border_width => 0
	);

    $img_s = $img_s->scale( ypixels => 150 );

    $img_s->filter(
	type      => 'contrast',
	intensity => 1.1,
	);
    $img_m->filter(
	type      => 'contrast',
	intensity => 1.1,
	);

    $img_s->filter(type=>"conv", coef=>[-1, 4, -1 ])
	or die $img_s->errstr;
    $img_m->filter(type=>"conv", coef=>[-1, 4, -1 ])
	or die $img_m->errstr;
    
    $img_s->write( file => "$output_dir/thumbnail_s/${filename}", jpegquality => 100 )
	or die $img_s->errstr;
    $img_m->write( file => "$output_dir/thumbnail_m/${filename}", jpegquality => 100 )
	or die $img_m->errstr;
}

my $template = new Template();

my $param = {
    photos => \@photos,
};

$template->process( 'tmpl/index.tmpl', $param );
